package com.chenxin.wms.process.inbound.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.wms.IbMasterType;
import com.chenxin.common.exception.wms.WmsException;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.IbDetailEntity;
import com.chenxin.wms.domain.IbMasterEntity;
import com.chenxin.wms.excel.IbMasterWipExcel;
import com.chenxin.wms.mapper.IbDetailMapper;
import com.chenxin.wms.mapper.IbMasterMapper;
import com.chenxin.wms.process.inbound.IIbMasterImportProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class IbMasterImportImpl implements IIbMasterImportProcess {
    @Autowired
    private IbMasterMapper ibMasterMapper;
    @Autowired
    private IbDetailMapper ibDetailMapper;

    @Override
    public ProcResult wipImport(List<IbMasterWipExcel> details, boolean updateSupport, Long creator) {
        if (StringUtils.isNull(details) || details.size() == 0) {
            return ProcResult.error("import_error001", "导入单据数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        // 统计销售订单数量
        List<String> ebelns = details.stream().map(IbMasterWipExcel::getEbeln)
                .filter(Objects::nonNull)
                .distinct().collect(Collectors.toList());
        if (ebelns.size() == 0) {
            return ProcResult.error("import_error001", "导入单据数据【销售订单】不能为空");
        }
        Date workDate = new Date();
        // 循环处理单据信息
        for (String ebeln : ebelns) {
            // 判断主单单据信息是否存在
            QueryWrapper<IbMasterEntity> qw = new QueryWrapper<>();
            qw.lambda().eq(IbMasterEntity::getEbeln, ebeln)
                    .lt(IbMasterEntity::getIbMasterStatus, 4)
                    .last("limit 1");
            IbMasterEntity existEbeln = ibMasterMapper.selectOne(qw);
            if (existEbeln != null) {
                throw new WmsException("mes_in_err001", "数据导入失败，采购单【" + ebeln + "】已存在！");
            }
            List<IbMasterWipExcel> detailsByEbeln = details.stream().filter(d -> ebeln.equals(d.getEbeln())).collect(Collectors.toList());
            // 处理主单信息
            IbMasterEntity ibMaster = new IbMasterEntity();
            // 时间戳定义编号
            ibMaster.setIbMasterNo(String.valueOf(System.currentTimeMillis()));
            ibMaster.setEbeln(ebeln);
            // 原料收货登记
            ibMaster.setIbMasterType(IbMasterType.IN_WIP.getCode());
            // 手动导入单据
            ibMaster.setFromErp("0");
            ibMaster.setCreator(creator);
            ibMaster.setCreateDate(workDate);
            ibMasterMapper.insert(ibMaster);
            for (IbMasterWipExcel detail : detailsByEbeln) {
                IbDetailEntity ibDetail = new IbDetailEntity();
                ibDetail.setIbMasterId(ibMaster.getIbMasterId());
                ibDetail.setIbMasterNo(ibMaster.getIbMasterNo());
                // 订单号
                ibDetail.setEbeln(ebeln);
                // 订单行号
                ibDetail.setEbelp(detail.getEbelp());
                ibDetail.setSpuNo(detail.getSpuNo());
                ibDetail.setLotNo(detail.getLotNo());
                ibDetail.setIbPlanQty(detail.getIbPlanQty());
                ibDetail.setLgort(detail.getLgort());
                ibDetail.setCreator(creator);
                ibDetail.setCreateDate(workDate);
                ibDetailMapper.insert(ibDetail);
                successNum++;
            }
        }
        return ProcResult.success("数据导入成功！共【" + successNum + "】条");
    }
}
